package com.inossem.utils.chart;

import android.app.Activity;
import android.graphics.Color;

import com.inossem.utils.base.ArithUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2018/1/5.
 */

public class ChartUtils {

    public static final String COLOR_PINK = "#FFC0CB";

    public static final String COLOR_PURPLE = "#9932CC";

    public static final String COLOR_BLUE = "#4169E1";

    public static final String COLOR_CYAN = "#008B8B";

    public static final String COLOR_GREEN = "#7FFFAA";

    public static final String COLOR_GOLD = "#FFD700";

    public static final String COLOR_YELLOW = "#FFFF00";

    public static final String COLOR_ORANGE = "#FFA500";

    public static final String COLOR_RED = "#CD5C5C";

    public static final String COLOR_GRAY = "#808080";

    public static final String COLOR_BROWN = "#CD853F";

    public static final String COLOR_OLIVE = "#808000";

    public static final String TYPE_BAR = "0";

    public static final String TYPE_LINE = "1";

    public static final String DEPENDENCE_LEFT = "0";

    public static final String DEPENDENCE_RIGHT = "1";

    public static final float DEFULT_GROUP_SPACE = 0.1F;

    public static final float DEFULT_BAR_SPACE = 0.0F;

    public static final String KEY_INFORMATION = "Key_Information";

    public static final String KEY_SELECT_CLASS_NAME = "Key_Select_Class_Name";

    public static final String KEY_PRIMARY_KEY = "Key_PRIMARY_KEY";

    private List<Integer> colors;

    public static float getBarWidth(int barCount, float groupSpace) {
        float totalSpace = 1F;
        String barsSpace = ArithUtils.sub(Float.toString(totalSpace), Float.toString(groupSpace)).toString();
        return ArithUtils.div(barsSpace, Integer.toString(barCount), 2, BigDecimal.ROUND_FLOOR).floatValue();
    }

    public static int getRGBFromHex(String hex) {
        int color = (int) Long.parseLong(hex.replace("#", ""), 16);
        int r = (color >> 16) & 0xFF;
        int g = (color >> 8) & 0xFF;
        int b = (color >> 0) & 0xFF;
        return Color.rgb(r, g, b);
    }

    private void initColors() {
        colors = new ArrayList<>();
        colors.add(getRGBFromHex(COLOR_ORANGE));
        colors.add(getRGBFromHex(COLOR_PINK));
        colors.add(getRGBFromHex(COLOR_PURPLE));
        colors.add(getRGBFromHex(COLOR_BLUE));
        colors.add(getRGBFromHex(COLOR_GOLD));
        colors.add(getRGBFromHex(COLOR_CYAN));
        colors.add(getRGBFromHex(COLOR_GREEN));
        colors.add(getRGBFromHex(COLOR_RED));
        colors.add(getRGBFromHex(COLOR_YELLOW));
        colors.add(getRGBFromHex(COLOR_GRAY));
        colors.add(getRGBFromHex(COLOR_OLIVE));
        colors.add(getRGBFromHex(COLOR_BROWN));
    }

    public int getDefaultColor() {
        if (colors == null || colors.isEmpty()) {
            initColors();
        }
        Integer result = colors.get(0);
        colors.remove(result);
        return result;
    }

    public static String getPrimaryKey(Activity activity) {
        return activity.getIntent().getStringExtra(ChartUtils.KEY_PRIMARY_KEY);
    }

}
